/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tiango.baba.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.Spannable.Factory;
import android.text.style.ImageSpan;

import com.android.scsd.tiango.R;

public class SmileUtils {
	public static final String ee1 = "![ee1]";
	public static final String ee2 = "![ee2]";
	public static final String ee3 = "![ee3]";
	public static final String ee4 = "![ee4]";
	public static final String ee5 = "![ee5]";
	public static final String ee6 = "![ee6]";
	public static final String ee7 = "![ee7]";
	public static final String ee8 = "![ee8]";
	public static final String ee9 = "![ee9]";
	public static final String ee10 = "![ee10]";
	public static final String ee11 = "![ee11]";
	public static final String ee12 = "![ee12]";
	public static final String ee13 = "![ee13]";
	public static final String ee14 = "![ee14]";
	public static final String ee15 = "![ee15]";
	public static final String ee16 = "![ee16]";
	public static final String ee17 = "![ee17]";
	public static final String ee18 = "![ee18]";
	public static final String ee19 = "![ee19]";
	public static final String ee20 = "![ee20]";
	public static final String ee21 = "![ee21]";
	public static final String ee22 = "![ee22]";
	public static final String ee23 = "![ee23]";
	public static final String ee24 = "![ee24]";
	public static final String ee25 = "![ee25]";
	public static final String ee26 = "![ee26]";
	public static final String ee27 = "![ee27]";
	public static final String ee28 = "![ee28]";
	public static final String ee29 = "![ee29]";
	public static final String ee30 = "![ee30]";
	public static final String ee31 = "![ee31]";
	public static final String ee32 = "![ee32]";
	public static final String ee33 = "![ee33]";
	public static final String ee34 = "![ee34]";
	public static final String ee35 = "![ee35]";

	private static final Factory spannableFactory = Spannable.Factory.getInstance();

	private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

	static {

		addPattern(emoticons, ee1, R.drawable.ee1);
		addPattern(emoticons, ee2, R.drawable.ee2);
		addPattern(emoticons, ee3, R.drawable.ee3);
		addPattern(emoticons, ee4, R.drawable.ee4);
		addPattern(emoticons, ee5, R.drawable.ee5);
		addPattern(emoticons, ee6, R.drawable.ee6);
		addPattern(emoticons, ee7, R.drawable.ee7);
		addPattern(emoticons, ee8, R.drawable.ee8);
		addPattern(emoticons, ee9, R.drawable.ee9);
		addPattern(emoticons, ee10, R.drawable.ee10);
		addPattern(emoticons, ee11, R.drawable.ee11);
		addPattern(emoticons, ee12, R.drawable.ee12);
		addPattern(emoticons, ee13, R.drawable.ee13);
		addPattern(emoticons, ee14, R.drawable.ee14);
		addPattern(emoticons, ee15, R.drawable.ee15);
		addPattern(emoticons, ee16, R.drawable.ee16);
		addPattern(emoticons, ee17, R.drawable.ee17);
		addPattern(emoticons, ee18, R.drawable.ee18);
		addPattern(emoticons, ee19, R.drawable.ee19);
		addPattern(emoticons, ee20, R.drawable.ee20);
		addPattern(emoticons, ee21, R.drawable.ee21);
		addPattern(emoticons, ee22, R.drawable.ee22);
		addPattern(emoticons, ee23, R.drawable.ee23);
		addPattern(emoticons, ee24, R.drawable.ee24);
		addPattern(emoticons, ee25, R.drawable.ee25);
		addPattern(emoticons, ee26, R.drawable.ee26);
		addPattern(emoticons, ee27, R.drawable.ee27);
		addPattern(emoticons, ee28, R.drawable.ee28);
		addPattern(emoticons, ee29, R.drawable.ee29);
		addPattern(emoticons, ee30, R.drawable.ee30);
		addPattern(emoticons, ee31, R.drawable.ee31);
		addPattern(emoticons, ee32, R.drawable.ee32);
		addPattern(emoticons, ee33, R.drawable.ee33);
		addPattern(emoticons, ee34, R.drawable.ee34);
		addPattern(emoticons, ee35, R.drawable.ee35);
	}

	private static void addPattern(Map<Pattern, Integer> map, String smile, int resource) {
		map.put(Pattern.compile(Pattern.quote(smile)), resource);
	}

	/**
	 * replace existing spannable with smiles
	 * @param context
	 * @param spannable
	 * @return
	 */
	public static boolean addSmiles(Context context, Spannable spannable) {
		boolean hasChanges = false;
		for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
			Matcher matcher = entry.getKey().matcher(spannable);
			while (matcher.find()) {
				boolean set = true;
				for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(),
						ImageSpan.class))
					if (spannable.getSpanStart(span) >= matcher.start()
							&& spannable.getSpanEnd(span) <= matcher.end())
						spannable.removeSpan(span);
					else {
						set = false;
						break;
					}
				if (set) {
					hasChanges = true;
					spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(),
							matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				}
			}
		}
		return hasChanges;
	}

	public static Spannable getSmiledText(Context context, CharSequence text) {
		Spannable spannable = spannableFactory.newSpannable(text);
		addSmiles(context, spannable);
		return spannable;
	}

	public static boolean containsKey(String key) {
		boolean b = false;
		for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
			Matcher matcher = entry.getKey().matcher(key);
			if (matcher.find()) {
				b = true;
				break;
			}
		}

		return b;
	}

}
